/**
 * +======================================================================================================================+
 * @Author: CimZzz
 * @Date: 2022-09-07 18:23:34
 * 
 * 代码分割块，以下内容涉及: 
 * 
 * 针对消息队列的测试用例
 * 
 */
#include <cstdio>
#include "vl_message_queue.h"

using namespace VL;

int main(int argc, char** argv) {
	VLMessageQueue messageQueue;
	volatile bool isRunning = true;
	std::thread consumeThread1([&] {
		// 阻塞式获取队列数据
		while(isRunning) {
			auto dataPtr = messageQueue.dequeueBlock();
			if(dataPtr.get() != nullptr) {
				printf("thread1 recv: %d\n", *dataPtr->memoryCast<int>());
			}
			std::this_thread::sleep_for(std::chrono::seconds(1));
		}
	});
	std::thread consumeThread2([&] {
		// 非阻塞式获取队列数据
		while(isRunning) {
			auto dataPtr = messageQueue.dequeue();
			if(dataPtr.get() != nullptr) {
				printf("thread2 recv: %d\n", *dataPtr->memoryCast<int>());
			}
			std::this_thread::sleep_for(std::chrono::seconds(1));
		}
	});


	while(isRunning) {
		char c = getchar();
		switch(c) {
			case 'a':
				// 入队 1
				messageQueue.enqueue(1);
				break;
			case 'b':
				// 入队 2
				messageQueue.enqueue(2);
				break;
			case 'c':
				// 入队首 3
				messageQueue.enqueueFirst(3);
				break;
			case 'd':
				// 清空队列
				messageQueue.clear();
				break;
			case 'e':
				// 清空队列
				// 销毁队列
				messageQueue.destroy();
				break;
			case 'q':
				// 停止循环
				messageQueue.destroy();
				isRunning = false;
				break;
		}
	}

	if(consumeThread1.joinable()) {
		consumeThread1.join();
	}
	if(consumeThread2.joinable()) {
		consumeThread2.join();
	}

	return 0;
}